package net.ccfish.jvue.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import net.ccfish.common.mybatis.BaseMapper;
import net.ccfish.jvue.autogen.dao.JvueApiMapper;
import net.ccfish.jvue.autogen.model.JvueApi;
import net.ccfish.jvue.service.AclResourceService;
import net.ccfish.jvue.service.JvueApiService;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.WeekendSqls;


/**
 * Generated by Spring Data Generator on 31/01/2018
 */
@Service
@Transactional
public class JvueApiServiceImpl implements JvueApiService {

    private JvueApiMapper jvueApiMapper;
    
    @Autowired
    private AclResourceService aclResourceService;

    @Autowired
    public JvueApiServiceImpl(JvueApiMapper jvueApiMapper) {
        this.jvueApiMapper = jvueApiMapper;
    }

    @Override
    public BaseMapper<JvueApi> baseMapper() {
        return this.jvueApiMapper;
    }

    @Override
    public List<JvueApi> findByMemu(Integer pageId) {
        JvueApi jvueApi = new JvueApi();
        jvueApi.setPageId(pageId);
        return jvueApiMapper.select(jvueApi);
    }

    @Override
    public List<JvueApi> updateApisByMemu(Integer pageId, List<Integer> apis) {
        
        Assert.notNull(pageId, "画面ID不能为空");
        
        Collection<Integer> deleds;
        Collection<Integer> addeds;
        JvueApi jvueApi = new JvueApi();
        jvueApi.setPageId(pageId);
        List<JvueApi> jvueApis = jvueApiMapper.select(jvueApi);

        if (jvueApis != null) {
            if (apis != null && apis.size() > 0) {
                List<Integer> existsApis =
                        jvueApis.stream().map(api -> api.getApiCode()).collect(Collectors.toList());
                deleds = CollectionUtils.subtract(existsApis, apis);
                addeds = CollectionUtils.subtract(apis, existsApis);
            } else {
                // 删除所有
                jvueApiMapper.delete(jvueApi);
                return new ArrayList<>();
            }
        } else {
            deleds = null;
            addeds = apis;
        }

        if (deleds != null && deleds.size() > 0) {
            Example example = new Example.Builder(JvueApi.class)
                    .where(WeekendSqls.<JvueApi>custom().andEqualTo(JvueApi::getPageId, pageId)
                            .andIn(JvueApi::getApiCode, deleds))
                    .build();
            jvueApiMapper.deleteByExample(example);
        }
        
        if (addeds != null && addeds.size() > 0) {
            for (Integer a: addeds) {
                String name = aclResourceService.getName(a);
                JvueApi api = new JvueApi();
                api.setPageId(pageId);
                api.setApiCode(a);
                api.setName(name);
                jvueApiMapper.insertSelective(api);
            }
        }

        return findByMemu(pageId);
    }

    @Override
    public void deleteByPage(Integer pageId) {
        Assert.notNull(pageId, "画面ID不能为空");
        JvueApi jvueApi = new JvueApi();
        jvueApi.setPageId(pageId);
        jvueApiMapper.delete(jvueApi);
    }

}
